/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: abort.cpp
 *
 * Purpose: This section describes the APIs that constitute the explicit 
 *          abnormal termination facility
 *
 * Developer:
 *   wen.gu , 2020-04-20
 *
 * TODO:
 *
 ***************************************************************************/

/******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#include "ara/core/abort.h"

#include <mutex>
#include "ara/com/runtime/network_binding.h"
#include "ara/com/runtime/network_binding_skeleton.h"
#include "ara/com/skeleton/service_skeleton.h"
#include "ara/com/skeleton/feild.h"
#include "ara/com/skeleton/event.h"
#include "ara/core/promise.h"
namespace ara
{
namespace core
{
/******************************************************************************
 **    MACROS
 ******************************************************************************/



/******************************************************************************
 **    TYPE DEFINITIONS
 ******************************************************************************/
static AbortHandler gAbortHandler = nullptr;
/******************************************************************************
 **  inner  CLASSES/FUNCTIONS DEFINITIONS
 ******************************************************************************/
ara::com::skeleton::feild::Feild<int, true, true, true> f1(nullptr, "string");


/******************************************************************************
 **    CLASSES/FUNCTIONS DEFINITIONS
 ******************************************************************************/
#if 1
ara::core::Future<int> F1Get()
{
    /** todo */

    ara::core::Promise<int> pr;
    return pr.get_future();

}

ara::core::Future<char> F1Get2(ara::core::Future<int>&& r)
{
    /** todo */
    ara::core::Promise<char> pr;
    return pr.get_future();

}

ara::core::Future<int> F1Set(int val)
{
    /** todo */
    ara::core::Promise<int> pr;
    ara::core::Future<int> f = pr.get_future();

    ara::core::Future<Future<char>>  ret = f.then(F1Get2);

    return pr.get_future();
}
#endif

AbortHandler SetAbortHandler(AbortHandler handler) noexcept
{
    
    f1.RegisterGetHandler(F1Get);
    //f1.RegisterSetHandler(F1Set);
    //f1.Update(2);
    //ara::core::Promise<int> pr;
    //std::promise<ara::core::Result<int>> pr;

    std::shared_ptr<ara::core::Promise<bool>> find_promise = std::make_shared<ara::core::Promise<bool>>();


    static std::mutex Lock;
    AbortHandler prevHandler;
    Lock.lock();
    prevHandler = gAbortHandler;
    gAbortHandler = handler;
    Lock.unlock();

    return prevHandler;

}


void Abort(char const* text) noexcept
{
    static std::mutex Lock;
    std::unique_lock<std::mutex> al(Lock);

    /** todo, record log with fatal level, */
    // log(FATAL, text);
    if (gAbortHandler)
    {
        gAbortHandler();
    }
    std::abort();

}


} /** namespace core */
} /** namespace ara */